package cn.birdbaby.plugins.deletegen;

import cn.birdbaby.plugins.common.CommonUtils;
import com.intellij.codeInsight.actions.LayoutCodeOptions;
import com.intellij.codeInsight.actions.TextRangeType;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author llorando 4/8/24 9:40 am
 */
public class BBDeleteGenAnnotationsProcessor {
    private DataContext dataContext;
    private Project project;
    private Editor editor;
    private Document document;

    private PsiFile psiFile;
    private List<PsiFile> psiFiles;
    private LayoutCodeOptions options;
    private List<PsiAnnotation> genAnnotations;

    public BBDeleteGenAnnotationsProcessor(Project project, Editor editor, PsiFile psiFile) {
        this.psiFile = psiFile;
        this.project = project;
        this.editor = editor;
        this.options = options;
        this.genAnnotations = new ArrayList<>();
    }

    public BBDeleteGenAnnotationsProcessor(Project project, Editor editor, LayoutCodeOptions options, List<PsiFile> files) {
        this.psiFiles = files;
        this.project = project;
        this.editor = editor;
        this.options = options;
        this.genAnnotations = new ArrayList<>();

    }

    public void processCode() {
        if (!Objects.isNull(psiFile)) {
            processSingleFile();
        } else {
            processFiles();
        }

    }

    public void processSingleFile() {
        Collection<PsiElement> gens= CommonUtils.collectGenElement(psiFile);
        if (CollectionUtils.isEmpty(gens)) return;

        //单个删除的话，在回退栈中，是一个个Gen的操作记录，而不是一组
        WriteCommandAction.
                runWriteCommandAction(project, () -> {
                    gens.forEach(PsiElement::delete);
        });
//        PsiDocumentManager.getInstance(project).commitDocument(document);

    }

    public void processFiles() {
        boolean isSuccess = ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
            return processFilesUnderProgress(indicator);
        }, "bb delete gen", true, project);

        System.out.println(isSuccess);
        //如果当前编辑器中存在打开的文件，更新一下
        if (editor != null && options.getTextRangeType() == TextRangeType.WHOLE_FILE) {
            CodeStyleSettingsManager.getInstance(project).notifyCodeStyleSettingsChanged();
        }
    }

    public boolean processFilesUnderProgress(@NotNull ProgressIndicator indicator) {
        indicator.setIndeterminate(false);
        BBProcessingDeleteGenAnnotationsTask processor=new BBProcessingDeleteGenAnnotationsTask(indicator,project);
        processor.process();
        return true;
    }

    public void collectGenAnnotation(Collection<PsiAnnotation> originalAnno) {
        for (PsiAnnotation psiAnnotation : originalAnno) {
            PsiJavaCodeReferenceElement nameReferenceElement = psiAnnotation.getNameReferenceElement();
            String referenceName = nameReferenceElement.getReferenceName();
            if (StringUtils.startsWithIgnoreCase(referenceName, "Gen")) {
                genAnnotations.add(psiAnnotation);
            }
        }
    }


}
